package cn.yuanqiao.web.controller.archive.liyong;

import cn.yuanqiao.archive.liyong.domain.YqConsultRegister;
import cn.yuanqiao.archive.liyong.domain.YqDaRegister;
import cn.yuanqiao.archive.liyong.service.IYqConsultRegisterService;
import cn.yuanqiao.common.utils.ip.IpUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.core.context.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
 * 查档登记表Controller
 *
 * @ProjectName: cn.yuanqiao.archive.liyong.controller
 * @ClassName: YqConsultRegisterController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2024/1/26 16:54
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/archive/consult/register")
public class YqConsultRegisterController extends BaseController {
    @Autowired
    private IYqConsultRegisterService yqConsultRegisterService;

    /**
     * 查询查档登记表列表
     */
    // @PreAuthorize("@ss.hasPermi('archive:consultRegister:list')")
    @GetMapping("/list")
    public TableDataInfo list(YqConsultRegister yqConsultRegister) {
        startPage();
        List<YqConsultRegister> list = yqConsultRegisterService.selectYqConsultRegisterList(yqConsultRegister);
        return getDataTable(list);
    }

    /**
     * 查询登记档案信息列表
     */
    //  @PreAuthorize("@ss.hasPermi('archive:consultRegister:dalist')")
    @GetMapping("/da/list")
    public TableDataInfo list(YqDaRegister yqDaRegister) {
        startPage();
        List<YqDaRegister> list = yqConsultRegisterService.selectYqDaRegisterList(yqDaRegister);
        return getDataTable(list);
    }


    /**
     * 导出查档登记表列表
     */
    //   @PreAuthorize("@ss.hasPermi('archive:consultRegister:export')")
    @Log(title = "查档登记表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqConsultRegister yqConsultRegister) {
        List<YqConsultRegister> list = yqConsultRegisterService.selectYqConsultRegisterList(yqConsultRegister);
        ExcelUtil<YqConsultRegister> util = new ExcelUtil<YqConsultRegister>(YqConsultRegister.class);
        util.exportExcel(response, list, "查档登记表数据");
    }

    /**
     * 获取查档登记表详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('archive:consultRegister:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(yqConsultRegisterService.selectYqConsultRegisterById(id));
    }

    /**
     * 提交
     */
    //  @PreAuthorize("@ss.hasPermi('archive:consultRegister:add')")
    @Log(title = "查档登记表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqConsultRegister yqConsultRegister) {
        return toAjax(yqConsultRegisterService.insertYqConsultRegister(yqConsultRegister));
    }

    /**
     * 添加到档案信息登记
     *
     * @param yqDaRegister
     * @return
     */
    // @PreAuthorize("@ss.hasPermi('archive:consultRegister:add')")
    @Log(title = "查档登记表", businessType = BusinessType.INSERT)
    @PostMapping("/addDa")
    public AjaxResult addDa(@RequestBody YqDaRegister yqDaRegister) {
        return toAjax(yqConsultRegisterService.insertYqDaRegister(yqDaRegister));
    }

    /**
     * 修改查档登记表
     */
    //  @PreAuthorize("@ss.hasPermi('archive:consultRegister:edit')")
    @Log(title = "查档登记表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqConsultRegister yqConsultRegister) {
        return toAjax(yqConsultRegisterService.updateYqConsultRegister(yqConsultRegister));
    }

    /**
     * 删除查档登记表
     */
    // @PreAuthorize("@ss.hasPermi('archive:consultRegister:remove')")
    @Log(title = "查档登记表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(yqConsultRegisterService.deleteYqConsultRegisterByIds(ids));
    }

    /**
     * 删除登记档案信息
     */
    // @PreAuthorize("@ss.hasPermi('archive:登记档案信息:remove')")
    @Log(title = "登记档案信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/delDa/{ids}")
    public AjaxResult removeDa(@PathVariable String[] ids) {
        return toAjax(yqConsultRegisterService.deleteYqDaRegisterByIds(ids));
    }

    /**
     * 文件预览
     *
     * @param response
     * @param id
     * @throws Exception
     */
    @PostMapping("/preview")
    public void view(HttpServletResponse response, @RequestParam("id") String id)
            throws Exception {
        byte[] bytes = yqConsultRegisterService.queryImage(id);
        // 以流的形式返回
        ServletOutputStream out = null;
        ByteArrayOutputStream baos = null;
        try {
            InputStream inStream = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.reset(); // 非常重要
            // 设置 CORS 响应头
            ResponseUtils.defaultResponse(response);
            //在线预览，浏览器支持的文件类型都能打开
            response.setContentType("application/octet-stream");
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Objects.requireNonNull(baos).flush();
            baos.close();
            Objects.requireNonNull(out).flush();
            out.close();
        }
    }

    /**
     * 获取ip地址
     */
    // @PreAuthorize("@ss.hasPermi('archive:登记档案信息:remove')")
    @PostMapping("/host/ip")
    public AjaxResult hostIp() {
        return AjaxResult.success(IpUtils.getHostIp());
    }

}
